package com.leoman.plz_p.views.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.MistakeSubjectAdapter;
import com.leoman.plz_p.interfaces.MistakeSubjectView;
import com.leoman.plz_p.module.bean.MistakeSubjectBean;
import com.leoman.plz_p.presenter.MistakeSubjectPresenter;
import com.leoman.plz_p.views.study.AnswerActivity;
import com.leoman.plz_p.weidget.NavBar;

import java.util.List;

import butterknife.Bind;
import cn.com.corelib.base.BaseActivity;

/**
 * Created by guoh on 2018/9/2.
 */
public class MistakeActivity extends BaseActivity<MistakeSubjectView,MistakeSubjectPresenter> implements MistakeSubjectView{

    @Bind(R.id.nav)
    NavBar nav;
    @Bind(R.id.iv_mistake_br)
    ImageView ivMistakeBr;
    @Bind(R.id.tv_mistake_num)
    TextView tvMistakeNum;
    @Bind(R.id.ls_mistake)
    ListView lsMistake;

    private MistakeSubjectAdapter adapter;

    public static Intent getLaunchIntent(Context context) {
        Intent intent = new Intent(context, MistakeActivity.class);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_mistake_subject;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        nav.setTitle("我的错题");
        ivMistakeBr.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ivMistakeBr.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivMistakeBr.getLayoutParams();
                params.height = (int) (ivMistakeBr.getMeasuredWidth() / 1.78);
                ivMistakeBr.setLayoutParams(params);

            }
        });

        adapter = new MistakeSubjectAdapter(getViewContext());
        lsMistake.setAdapter(adapter);
        lsMistake.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(AnswerActivity.getLaunchIntent(getViewContext(), adapter.getItem(i).getTitle(), adapter.getItem(i).getTopicIds(), null,0, true, false));
            }
        });

    }

    @Override
    protected MistakeSubjectPresenter createPresenter() {
        return new MistakeSubjectPresenter();
    }


    @Override
    public void obtainMistakeList(String total, List<MistakeSubjectBean> data) {
        tvMistakeNum.setText(total);
        adapter.replaceAll(data);
    }

    @Override
    public void obtainAllMistake(String ids) {
        startActivity(AnswerActivity.getLaunchIntent(getViewContext(), "全部错题",ids, null,0, true, false));
    }



}
